package com.cupk.controller;

import com.cupk.pojo.Match;
import com.cupk.pojo.Result;
import com.cupk.service.MatchService;

import com.cupk.service.StageService;
import com.cupk.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/match")
public class MatchController {
    @Autowired
    private MatchService matchService;
    @Autowired
    private StageService stageService;

    @GetMapping("/list")
    public Result<List<Match>> list(@RequestParam String name) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        Integer role = (Integer) map.get("role");
        List<Match> ls;
        if (role == 0) {
            ls = matchService.getAllList(name);
        } else {
            ls = matchService.getMyList(userId, name);
        }
        for (Match m : ls) {
            Integer maxStageId = stageService.findMaxStage(m.getId());
            Integer minStageId = stageService.findMinStage(m.getId());
            Integer alreadyPeople = stageService.getAcceptPeople(maxStageId);
            Integer doingPeople = stageService.getUnAcceptPeople(minStageId);
            m.setCountEnd(alreadyPeople);
            m.setCountStart(doingPeople);
        }
        return Result.success(ls);
    }

    @GetMapping("/detail")
    public Result<Match> detail(@RequestParam Integer id) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        Integer role = (Integer) map.get("role");
        Match match = matchService.getMatchById(id);
        if (match.getUser() != userId && role != 0) {
            return Result.error("比赛ID请求非法！");
        }
        Integer maxStageId = stageService.findMaxStage(match.getId());
        Integer minStageId = stageService.findMinStage(match.getId());
        Integer alreadyPeople = stageService.getAcceptPeople(maxStageId);
        Integer doingPeople = stageService.getUnAcceptPeople(minStageId);
        match.setCountEnd(alreadyPeople);
        match.setCountStart(doingPeople);
        return Result.success(match);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Match match) {
        String name = match.getName();
        Integer session = match.getSession();
        List<Match> ls = matchService.findSimilar(name, session);
        if (ls.size() > 0) {
            return Result.error("当前比赛已存在或已被其他用户创建，请联系贾志洋老师处理！");
        }
        matchService.addStatus(match);
        matchService.addMatch(match);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Match match) {
        Match res = matchService.getMatchById(match.getId());
        if (res == null) {
            return Result.error("比赛ID请求非法！");
        }
        matchService.addStatus(match);
        matchService.updateMatch(match);
        return Result.success();
    }

    @DeleteMapping("/del")
    public Result deleteMatch(@RequestParam Integer id) {
        Match res = matchService.getMatchById(id);
        if (res == null) {
            return Result.error("比赛ID请求非法！");
        }
        matchService.deleteMatch(id);
        return Result.success();
    }

    @GetMapping("/availableList")
    public Result<List<Match>> availableMatchList() {
        LocalDateTime nowTime = LocalDateTime.now();
        List<Match> ls = matchService.getAvailableList(nowTime);
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        Integer status = matchService.judgeBlackList(userId);
        if (status == 1) {
            return Result.error("您因比赛违纪被拉入黑名单！");
        }
        return Result.success(ls);
    }

    @GetMapping("/getDetail")
    public Result<Match> getDetail(@RequestParam Integer id) {
        Match res = matchService.getMatchById(id);
        if (res == null) {
            return Result.error("比赛ID请求非法！");
        }
        return Result.success(res);
    }
}
